package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepsitory;
import cn.wolfcode.wolf2w.repository.RegionRepsitory;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationRepsitory repsitory;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(Destination destination) {
        repsitory.save(destination);
    }

    @Override
    public void update(Destination destination) {
        repsitory.save(destination);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repsitory.findAll();
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {


        //获取区域对象
        Region region = regionService.get(rid);

        //通过区域对象获取管理目的id集合
        List<String> ids = region.getRefIds();


        //ids ---> 目的地集合: select * from xxx where id in (1,2,3,4)
        return repsitory.findByIdIn(ids);
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String rid) {

        //区域挂载的目的地
        List<Destination> list = null;
        if("-1".equals(rid)){
            //查询国内--查询中国所有省份
            list = repsitory.findByParentName("中国");
        }else{
            list = this.queryDestByRegionId(rid);
        }
        //目的地下的子目的地
        //台湾  香港
        for (Destination dest : list) {
            //台湾下的儿子....
            //要求: 儿子只需要前5个
            Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "_id");
            List<Destination> children = repsitory.findByParentId(dest.getId(),pageable);
            //截取:children.subList(0, 5)
            dest.setChildren(children);
        }
        return list;
    }



    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();
        //条件
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        //父id
        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        //list
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,pageable, query, Destination.class, qo);
    }



    //例子: 根 >> 中国 >> 广东 >> 广州
    @Override
    public List<Destination> queryToasts(String destId) {
        List<Destination> list = new ArrayList<>();
        //吐司查询

        //广州 -- 广东 --- 中国
        //1: while(老爸id不为null...)
        //2:递归
        this.createToast(list, destId);
        //list集合元素顺序调转
        Collections.reverse(list);

        return list;
    }
    //例子: 根 >> 中国 >> 广东 >> 广州
    private void createToast(List<Destination> list, String destId){
        //递归前提: 必须能出
        if(!StringUtils.hasLength(destId)){
            return;
        }
        Destination dest = this.get(destId);  //
        list.add(dest);

        if(StringUtils.hasLength(dest.getParentId())){
            createToast(list, dest.getParentId());
        }
    }


    @Override
    public Destination queryByName(String name) {
        return repsitory.findByName(name);
    }
}
